<?php
    
    namespace App\Http\Controllers\Home;
    
    use App\Models\Attribute;
    use App\Models\AttributeClassify;
    use App\Models\Carousel;
    use App\Models\Classify;
    use App\Models\Collect;
    use App\Models\Goods;
    use App\Models\Product;
    use App\Models\Track;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    
    class IndexController extends Controller
    {
        public function __construct()
        {
           $this->middleware('auth.login',['except'=>['index','list']]);
          
        }
    
        public function index()
        {
            dd(11);
            $carousels = Carousel::where('sign', '首页轮播图')->where('status', 1)->value('icon');
            
            $advertisement = Carousel::where('sign', '首页广告图')->value('icon');
            //新品数据
            $newGoods = Goods::orderBy('created_at', 'DESC')->get();
            
         $bigData=getCategoryGoods()->toArray();
            return view('home.index.index', compact('advertisement', 'carousels', 'newGoods','bigData'));
        }
        
        public function list(Request $request, Classify $classify, $num = 0)
        {
            $minprice = $request->minprice;
            $maxprice = $request->maxprice;
            // 找出商品表中的所有商品
            $goodsIds = $classify->getGoods($classify, $request->attribute, $minprice, $maxprice);
            // 再从商品表中找出id再上面数组中的商品数据,分页
            $goodList = Goods::whereIn('id', $goodsIds)->paginate($num ? intval($num) : 20);
            //获取不同分类下时间最新的五件商品
            $newGood = Goods::whereIn('id', $goodsIds)->orderBy('created_at', 'desc')->limit(5)->get();
            //获得分类下的商品属性,
            //如果点击的分类是一级就要的到他下面的所有三级的属性
            if ($classify->level == 1) {
//                先获得二级
                $secondClassify = Classify::where('pid', $classify->id)->get();
//                dd($secondClassify);
                $thirdClassifys = [];
                foreach ($secondClassify as $value) {
//                    dump($value);
//                    再获取他的三级分类
                    $thirdClassify = Classify::where('pid', $value->id)->get();
                    foreach ($thirdClassify as $item) {
                        $thirdClassifys[] = $item;
                    }
                }
                //获取三级分类后获得他的属性,合并数组再去重
                $attributes = [];
                foreach ($thirdClassifys as $v) {
                    $attribute = AttributeClassify::where('classify_id', $v->id)->pluck('attribute_id')->toArray();
                    $attributes = array_unique(array_merge($attributes, $attribute));
                }
//                获取属性的对象
                $attributeArr = Attribute::whereIn('id', $attributes)->get();
            }
//            如果点击的是二级分类
            if ($classify->level == 2) {
                //获取他的三级分类
                $thirdClassify = Classify::where('pid', $classify->id)->get();
                $attributes = [];
                foreach ($thirdClassify as $v) {
                    $attribute = AttributeClassify::where('classify_id', $v->id)->pluck('attribute_id')->toArray();
                    $attributes = array_unique(array_merge($attributes, $attribute));
                }
//                获取属性的对象
                $attributeArr = Attribute::whereIn('id', $attributes)->get();
            }
            //当点击的是三级分类.获得三级分类下的属性
            if ($classify->level == 3) {
                $attribute = AttributeClassify::where('classify_id', $classify->id)->pluck('attribute_id')->toArray();
//                获取属性的对象
                $attributeArr = Attribute::whereIn('id', $attribute)->get();
            }
            return view('home.index.list', compact('classify', 'goodList', 'num', 'attributeArr', 'newGood'));
        }
        
        public function showGoods(Goods $good)
        {
            //在用户点击商品进入详情页后,将该商品加入足迹表中
            $tracks = Track::where('goods_id', $good->id)->where('user_id', auth()->user()->id)->first();
            if (!$tracks) {
                Track::create([
                    'user_id' => auth()->user()->id,
                    'goods_id' => $good->id,
                ]);
            }
//            获取当前商品的货品的第一条
            $product = Product::where('goods_id', $good->id)->first();
//         读取收藏表数据
            $collect = Collect::where('user_id', auth()->user()->id)->where('goods_id', $good->id)->first();
            return view('home.index.goods', compact('good', 'product', 'collect'));
        }
        
        //快速查看
        public function quickView(Goods $good)
        {
            $tracks = Track::where('goods_id', $good->id)->where('user_id', auth()->user()->id)->first();
            if (!$tracks) {
                Track::create([
                    'user_id' => auth()->user()->id,
                    'goods_id' => $good->id,
                ]);
            }
            $collect = Collect::where('user_id', auth()->user()->id)->where('goods_id', $good->id)->first();
            $product = Product::where('goods_id', $good->id)->first();
            return view('home.index.quickView', compact('good', 'product', 'collect'));
        }
    }
